﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;

namespace WindowsFormsApplication1
{
    
    public partial class Filtros : Form
    {
        string dest = "";

        public Filtros()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string directorio_actual = Directory.GetCurrentDirectory();
            DialogResult result = openFileDialog1.ShowDialog();
            if (result == DialogResult.OK)
            {
                FileInfo archivo = new FileInfo(openFileDialog1.FileName);
                
                string dest = Application.LocalUserAppDataPath + "\\" +archivo.Name;

                string ruta_del_archivo = archivo.FullName;

            try {
                File.Copy(ruta_del_archivo, dest, true);
            } catch (Exception ex) {
                Console.WriteLine(ex);
            }
                pictureBox1.WaitOnLoad = true;
                pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
                pictureBox2.SizeMode = PictureBoxSizeMode.StretchImage;
                pictureBox3.SizeMode = PictureBoxSizeMode.StretchImage;
                pictureBox1.Load(dest);
                textBox1.Text = dest;
                this.dest = dest;
            }
            Directory.SetCurrentDirectory(directorio_actual);
        }

        private void armarComando(bool procesarSilueta) {
            String command = " -i \"" + this.dest + "\" -o \"" + Application.LocalUserAppDataPath + "\\" + textBox5.Text + "\"";

            if (checkBox1.Checked) command = command + " -B";
            if (checkBox2.Checked) command = command + " -b " + textBox4.Text;
            if (checkBox3.Checked) command = command + " -c " + numericUpDown1.Value;
            if (checkBox4.Checked) command = command + " -C " + textBox6.Text + " " + textBox7.Text + " " + textBox8.Text;
            if (checkBox5.Checked) command = command + " -f 1";
            if (checkBox13.Checked) command = command + " -f 0";
            if (checkBox6.Checked) command = command + " -g " + textBox10.Text;
            if (checkBox7.Checked) command = command + " -I";
            if (checkBox8.Checked) command = command + " -L";
            if (checkBox9.Checked) command = command + " -m";
            if (checkBox10.Checked) command = command + " -S";
            if (checkBox11.Checked) command = command + " -T " + textBox3.Text;
            if (checkBox14.Checked) command = command + " -a";

            if (procesarSilueta)
            {
                command = command + " -s ";
                if (checkBox12.Checked) command = command + "1";
                else command = command + "0";
                command = command + " " + textBox9.Text;
            }

            textBox2.Text = command;
            bool result=this.procesar(command, "taller2.exe");
            if (result)
            {
                pictureBox2.Load(Application.LocalUserAppDataPath + "\\" + textBox5.Text);

                if (procesarSilueta)
                {
                    listBox1.Items.Clear();

                    string[] lines = File.ReadAllLines("soluciones.txt");
                    var items =
                    from line in lines
                    select new
                    {
                        Bit = line.Split(new char[] { ';' })[0],
                        Name = line.Split(new char[] { ';' })[1]
                    };

                    foreach (var item in items)
                    {
                        listBox1.Items.Add(item.Bit);
                    }
                    if (listBox1.Items.Count > 0) listBox1.SelectedIndex = 0;
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.armarComando(true);
        }

        public bool procesar(String command, String filename)
        {
            string Arguments = command;
            Process Process = new Process();
            ProcessStartInfo StartInfo = new ProcessStartInfo();

            StartInfo.FileName = filename + " ";

            StartInfo.RedirectStandardError = false;
            StartInfo.RedirectStandardOutput = false;
            StartInfo.RedirectStandardInput = false;

            StartInfo.UseShellExecute = false;
            StartInfo.CreateNoWindow = true;

            StartInfo.Arguments = Arguments;

            Process.EnableRaisingEvents = true;
            Process.StartInfo = StartInfo;

            Process.Start();

            Process.WaitForExit(150000);

            int ExitCode = Process.ExitCode;
            bool Successful = true;

            if (ExitCode > 0 & !Process.HasExited)
            {
                Process.Kill();
                Successful = false;
            }

            //now clean up after ourselves
            Process.Dispose();
            StartInfo = null;
            return Successful;
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            pictureBox3.Load(Application.LocalUserAppDataPath + "\\" + listBox1.SelectedItem.ToString());
        }

        private void button4_Click(object sender, EventArgs e)
        {
            this.armarComando(false);
        }

        private void button5_Click(object sender, EventArgs e)
        {
            procesar("", "indexar.exe");
        }

        private void button6_Click(object sender, EventArgs e)
        {
            File.Delete("datos/coeficientes.dat");
            File.Delete("datos/meta.dat");
            File.Delete("datos/indice.dat");
            File.Delete("datos/pivotes.dat");
        }
    }
}
